package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Permission;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 翟文海
 * @Date: 2022/10/25/025 12:37
 * @Version: 1.0
 * @Description:
 **/
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    private final static String PAGE_INDEX = "role/index";
    private final static String PAGE_EDIT = "role/edit";
    private static final String SUCCESS_PAGE = "common/successPage";
    private static final String LIST_ROLE = "redirect:/role";
    private static final String PAGE_ASSIGN_SHOW = "role/assignShow";
    private static final String PAGE_CREAT = "role/create";

    @Reference
    private RoleService roleService;

    @Reference
    private PermissionService permissionService;

    @PreAuthorize("hasAnyAuthority('role.show')")
    @RequestMapping
    public String index(@RequestParam Map<String, String> filters, Model model) {
        /*
         * 1.获取请求参数：PageSize和PageNum，查询条件
         * 2.返回查询结果：总记录数，当前页，页码PageInfo，回显查询条件
         * */
        PageInfo<Role> pageInfo = roleService.findPage(filters);
        model.addAttribute("page", pageInfo);
        model.addAttribute("filters", filters);
        return PAGE_INDEX;
    }

    @PreAuthorize("hasAnyAuthority('role.show')")
    @GetMapping("/create")
    public String create(){
        return PAGE_CREAT;
    }
    @PreAuthorize("hasAnyAuthority('role.create')")
    @PostMapping("/save")
    public String save(Role role, Model model) {
        try {
            roleService.insert(role);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return toPage(model, "新增角色成功", SUCCESS_PAGE);
    }
    @PreAuthorize("hasAnyAuthority('role.show')")
    @GetMapping("/edit/{id}")
    public String edit(Model model, @PathVariable Long id) {
        Role role = roleService.getById(id);
        model.addAttribute("role", role);
        return PAGE_EDIT;
    }
    @PreAuthorize("hasAnyAuthority('role.edit')")
    @PostMapping("/update")
    public String update(Model model, Role role) {
        try {
            roleService.update(role);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return toPage(model, "更新角色成功", SUCCESS_PAGE);

    }
    @PreAuthorize("hasAnyAuthority('role.delete')")
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id, Model model) {
        try {
            roleService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return toPage(model, "删除角色成功", LIST_ROLE);
    }

    @PreAuthorize("hasAnyAuthority('role.show')")
    @GetMapping("/assignShow/{id}")
    public String assignShow(@PathVariable("id") Long id, Model model) {
        model.addAttribute("roleId", id);
        //查询所有的权限
        List<Map<String,Object>> zNodes = roleService.findPermissionByRoleId(id);
        model.addAttribute("zNodes", JSON.toJSONString(zNodes));
        return toPage(PAGE_ASSIGN_SHOW);
    }

    @PreAuthorize("hasAnyAuthority('role.assign')")
    @PostMapping("/assignPermission")
    public String assignPermission(Model model,@RequestParam("roleId") Long roleId,@RequestParam("permissionIds") List<Long> permissionIds) {
        roleService.saveRolePermission(roleId,permissionIds);
        return toPage(model,"分配权限成功",SUCCESS_PAGE);
    }
}
